﻿using SmartForm.Common;
using SmartlForm.Model;
using Dapper.Contrib.Extensions;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace SmartlForm.DAL
{
    public class ArticleDao
    {
        public readonly static ArticleDao Instance = new ArticleDao();
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageNo"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageHelper<Article> GetPageList(int pageNo, int pageSize, long accountId, string keyword)
        {
            var pageInfo = new PageHelper<Article>();
            var sql = string.Format("Select * From article WHERE account_id={0}", accountId);
            if (!string.IsNullOrEmpty(keyword))
            {
                sql += string.Format(" AND type like '%{0}%' OR title like '%{1}%' OR content LIKE  '%{2}%'", keyword, keyword, keyword);
            }
            pageInfo.Data = DataBase.DB.Query<Article>(string.Format("{0} ORDER BY id DESC Limit @Take Offset @Skip", sql), new { Take = pageSize, Skip = (pageNo - 1) * pageSize }).AsList();
            pageInfo.Total = DataBase.DB.QueryFirstOrDefault<int>(string.Format("select count(1) from ({0})", sql));
            pageInfo.PageNo = pageNo;
            pageInfo.PageSize = pageSize;
            return pageInfo;
        }


        /// <summary>
        /// 添加采集文章
        /// </summary>
        /// <param name="json"></param>
        /// <param name="accountId"></param>
        public void Add(string json, string type, long accountId)
        {
            List<Article> list = JsonConvert.DeserializeObject<List<Article>>(json);
            foreach (var item in list)
            {
                if (item == null || string.IsNullOrEmpty(item.title))
                {
                    continue;
                }
                if (GetByTitle(item.title) != null)
                {
                    Console.WriteLine("《" + item.title + "》：重复数据");
                }
                item.account_id = accountId;
                item.type = type;
                item.c_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                DataBase.DB.Insert(item);
            }
        }

        public Keyword GetByTitle(string title)
        {
            return DataBase.DB.QueryFirstOrDefault<Keyword>("SELECT * FROM article WHERE title=@title", new { title = title });
        }

        public List<GroupList> GetGroupByKeywordType(long account_id)
        {
            string sql = "SELECT type,count(1) as num FROM article WHERE account_id=@account_id GROUP BY type";
            return DataBase.DB.Query<GroupList>(sql, new { account_id = account_id }).AsList();
        }

        public long GetGount(long account_id)
        {
            return DataBase.DB.QueryFirstOrDefault<long>("select count(1) from article WHERE account_id=@account_id", new { account_id = account_id });
        }

        /// <summary>
        /// 今日数据
        /// </summary>
        /// <param name="account_id"></param>
        /// <returns></returns>
        public long GetNowGount(long account_id)
        {
            string c_time = DateTime.Now.ToString("yyyy-MM-dd");
            string sql = string.Format("select count(1) from article WHERE c_time LIKE '{0}%' AND account_id={1}", c_time, account_id);
            return DataBase.DB.QueryFirstOrDefault<long>(sql);
        }
    }

    public class GroupList
    {
        public string type { get; set; }
        public int num { get; set; }
    }
}
